// pages/project-list/project-list.js
import {
  activeCategory,
  featureService
} from '../../api/apis.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectList: [],
    pageIndex: 0,
    condition: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.did) {
      this.setData({
        categoryId: options.did,
        hosID: options.hosID || ""
      })
    } else {
      this.setData({
        categoryId: 0,
        hosID: options.hosID || ""
      })
    }
    this._getProject()
  },
  _getProject() {
    if (!this.data.hosID) {
      activeCategory({
        data: {
          index: ++this.data.pageIndex,
          size: 50,
          category: this.data.categoryId,
          condition: this.data.condition
        },
        success: res => {
          if (res.state) {
            let list = []
            res.data.list.forEach((item) => {
              list.push({
                img: item.FirstImg,
                name: item.title,
                supply: item.yiyuan,
                price: item.sku.length > 0 ? '￥' + item.sku[0].firstPay : '暂无价格',
                sales: item.sales,
                mid: item.IptId,
                SpecialOff: item.SpecialOff
              })
            })
            this.setData({
              projectList: this.data.projectList.concat(list)
            })
            if (this.data.projectList.length == res.data.count) {
              this.setData({
                noData: true
              })
            }
          }
        }
      })
    } else {
      featureService({
        data: {
          hospital: this.data.hosID,
          index: ++this.data.pageIndex,
          size: 50,
          condition: this.data.condition
        },
        success: res => {
          if (res.state) {
            if (res.data.list.length > 0) {
              let list = []
              res.data.list.forEach((item) => {
                list.push({
                  img: item.FirstImg,
                  name: item.title,
                  supply: item.yiyuan,
                  price: item.sku.length > 0 ? '￥' + item.sku[0].firstPay : '暂无价格',
                  sales: item.sales,
                  mid: item.IptId
                })
              })
              this.setData({
                projectList: this.data.projectList.concat(list)
              })
              if (this.data.projectList.length == res.data.count) {
                this.setData({
                  noData: true
                })
              }
            } else {
              this.setData({
                noData: true
              })
            }
          }
        }
      })
    }
  },
  onItemClick(e) {
    wx.navigateTo({
      url: `../project-detail/project-detail?did=${e.detail.item.mid}`,
    })
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  setSearchWords(e) {
    this.setData({
      condition: e.detail.value
    })
  },
  search() {
    this.data.pageIndex = 0
    this.data.projectList = []
    this._getProject()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this._getProject()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})